//
//  ZLZKViewController.m
//  ZLZKBaseUtils
//
//  Created by 1134548344@qq.com on 07/06/2020.
//  Copyright (c) 2020 1134548344@qq.com. All rights reserved.
//

#import "ZLZKViewController.h"
#import "ZLZKBaseBridge.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ZLZKViewController ()
<
    SXHBaseScriptMessageDelegate
>
//本地认证上下文联系对象
@property(nonatomic,strong) LAContext * context;

@end

@implementation ZLZKViewController
@dynamic url;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.url = @"https://www.zlzkyuxin.xyz/native-demo/";
//    self.url = @"https://yzf.qq.com/xv/web/static/chat/index.html";
//    self.url = @"http://10.37.6.112:8080";
//    self.url = @"http://192.168.1.113:8080";
//    self.url = @"http://192.168.0.108:8080";
//    self.url = @"http://10.43.80.97:8081";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)setUrl:(NSString *)url {

    [self setParams:@{
        @"name":@"张三",
        @"age": @(18)
    }.mutableCopy];
    #if TARGET_IPHONE_SIMULATOR  //模拟器
    #elif TARGET_OS_IPHONE //真机
        [self.webview addJavascriptObject:[speech new] namespaces:@"c"];
    #endif
    self.webview.delegate = self;
    [super setUrl:url];
}

- (void)demo {
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 120, 44)];
    [btn1 setTitle:@"开始录音" forState:0];
    [btn1 setBackgroundColor:[UIColor greenColor]];
    btn1.tag = 101;
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 120, 44)];
    [btn2 setTitle:@"结束录音" forState:0];
    [btn2 setBackgroundColor:[UIColor greenColor]];
    btn2.tag = 102;
    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 120, 44)];
    [btn3 setTitle:@"播放录音" forState:0];
    [btn3 setBackgroundColor:[UIColor greenColor]];
    btn3.tag = 103;
    [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
}

- (void)btnClick:(UIButton *)btn {
    if (btn.tag == 101) {
        [[SXHAVAudioManager shareInstace] startRecordNotice];
    }else if (btn.tag == 102) {
        [[SXHAVAudioManager shareInstace] stopRecordNotice];
    }else if (btn.tag == 103) {
        [[SXHAVAudioManager shareInstace] playRecordNotice];
    }
}

- (void)touchCheck {
    self.context = [[LAContext alloc] init];
    NSError *error = nil;
    BOOL canEvaluatePolicy = [_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
    if (error) {
        NSLog(@"%@",error.localizedDescription);
    }
    if (canEvaluatePolicy) {
        NSLog(@"有指纹识别功能");
        _context.localizedFallbackTitle = @"浩哥开门";
        [self beginTouchId];
    }else {
        NSLog(@"无指纹识别功能");
    }
    
}

- (void)beginTouchId {
    [_context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"验证指纹确认您的身份" reply:^(BOOL success, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (success) {
                NSLog(@"指纹识别成功");
            }else {
                NSLog(@"指纹识别失败,%@",error.description);
                NSLog(@"%ld",error.code);
                switch (error.code) {
                    case LAErrorUserCancel:
                        NSLog(@"用户取消");
                        break;
                    case LAErrorUserFallback:
                        NSLog(@"用户要输入密码");
                        break;
                    case LAErrorSystemCancel:
                        NSLog(@"用户取消了授权，比如其它应用切入了");
                        break;
                    case LAErrorPasscodeNotSet:
                        NSLog(@"用户就没有设置密码");
                        break;
                    case LAErrorBiometryNotAvailable:
                        NSLog(@"用户没有设置touch id");
                        break;
                    case LAErrorBiometryNotEnrolled:
                        NSLog(@"用户没设置职位");
                        break;
                    case LAErrorAppCancel:
                        NSLog(@"用户没有控制时app被挂起了");
                        break;
                    case LAErrorInvalidContext:
                        NSLog(@"context在之前已经失效了");
                        break;
                        
                    default:
                        NSLog(@"不知道啥情况");
                        break;
                }
            }
        });
    }];
}

- (void)webView:(nonnull SXHBaseWebView *)webView didReceiveScriptMessage:(nonnull SXHBaseScriptMessage *)scriptMessage {
    NSLog(@"调用的函数名：%@ \n调用的参数列表：%@", scriptMessage.name, scriptMessage.userInfo);
    
    if ([scriptMessage.name isEqual:@"Login"]) {
        [scriptMessage callbackWithRet:@{@"key":@"哦豁 你调用了登录方法诶"}];
    }
}


@end
